package server

import (
	"net/http"
	"strconv"
	"web-app/web-server/internal/config"
	"web-app/web-server/internal/context"
	"web-app/web-server/internal/route"
)

type WebServer struct {
	sev *http.Server
	ctx *context.Context
}

func NewWebServer(cfg *config.Config) *WebServer {
	addr := cfg.Server.Address + ":" + strconv.Itoa(cfg.Server.Port)
	ws := &WebServer{}
	srv := &http.Server{
		Addr:    addr,
		Handler: ws,
	}
	ws.sev = srv
	ws.ctx = &context.Context{
		Config: cfg,
	}
	return ws
}

func (ws *WebServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	route.WebRoute(ws.ctx, resp, req)
}

func (ws *WebServer) Start() {
	ws.sev.ListenAndServe()
}
